package GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Font;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.TableModel;

import Control.Ctr;
import Model.Subscription;

import javax.swing.JRadioButton;
import javax.swing.border.MatteBorder;

import java.awt.Color;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ListSelectionModel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ScrollPaneConstants;
import javax.swing.Box;
import javax.swing.border.LineBorder;
import javax.swing.SwingConstants;
public class Client {

	private JFrame frame;
	private JTextField textSSN;
	private JTextField textFname;
	private JTextField textLname;
	private JTextField textAddress;
	private JTextField textPhone;
	private JTextField textMID;
	private JTextField txtInputClientName;
	private JTable table;
	private JTextField txtSingleupdate;
	private JTextField textSession;
	private JTable table_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Client window = new Client();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Client() {
		
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		final Ctr ctr = new Ctr();
		frame = new JFrame();
		frame.setBounds(100, 100, 755, 491);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		final JLabel lblStatus = new JLabel("Status");
		lblStatus.setHorizontalAlignment(SwingConstants.CENTER);
		lblStatus.setFont(new Font("Arial", Font.BOLD, 14));
		lblStatus.setBounds(25, 430, 712, 23);
		frame.getContentPane().add(lblStatus);
		
		JLabel lblSsn = new JLabel("SSN");
		lblSsn.setBounds(25, 24, 80, 23);
		frame.getContentPane().add(lblSsn);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(25, 49, 80, 23);
		frame.getContentPane().add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(25, 74, 80, 23);
		frame.getContentPane().add(lblLastName);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(291, 24, 80, 23);
		frame.getContentPane().add(lblAddress);
		
		JLabel lblPhoneNumber = new JLabel("Phone number");
		lblPhoneNumber.setBounds(289, 49, 93, 23);
		frame.getContentPane().add(lblPhoneNumber);
		
		JLabel lblMemberid = new JLabel("Subscription");
		lblMemberid.setBounds(291, 74, 80, 23);
		frame.getContentPane().add(lblMemberid);
		
		
		
		textSSN = new JTextField();
		textSSN.setBounds(94, 25, 130, 20);
		frame.getContentPane().add(textSSN);
		textSSN.setColumns(10);
		
		textFname = new JTextField();
		textFname.setBounds(94, 50, 130, 20);
		frame.getContentPane().add(textFname);
		textFname.setColumns(10);
		
		textLname = new JTextField();
		textLname.setBounds(94, 75, 130, 20);
		frame.getContentPane().add(textLname);
		textLname.setColumns(10);
		
		textAddress = new JTextField();
		textAddress.setBounds(378, 25, 130, 20);
		frame.getContentPane().add(textAddress);
		textAddress.setColumns(10);
		
		textPhone = new JTextField();
		textPhone.setBounds(378, 50, 130, 20);
		frame.getContentPane().add(textPhone);
		textPhone.setColumns(10);
		
		textMID = new JTextField();
		textMID.setBounds(378, 75, 32, 20);
		frame.getContentPane().add(textMID);
		textMID.setColumns(10);
		
		JButton btnAddNewClient = new JButton("Add new Client");
		btnAddNewClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String ssn = textSSN.getText();
				String fname = textFname.getText();
				String address = textAddress.getText();
				int phone = Integer.parseInt(textPhone.getText());
				String lname = textLname.getText();
				int mID = Integer.parseInt(textMID.getText());	
				int session = Integer.parseInt(textSession.getText());
				int t =ctr.insertNewClient(ssn, fname, lname, address, phone, mID,session);
				Subscription n = ctr.findBymID(mID);
				int price = session * n.getPrice() - session * n.getDiscount();
				DateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
				Date date = new Date();
				String s = dateF.format(date);
				int z =ctr.insertNewInvoice(s, t, 0, 0, price);
				if(t!=-1)
					if(z!=-1)
					lblStatus.setText("Client created!");
					else
					lblStatus.setText("Failed!");
				}
			
		});
		btnAddNewClient.setBounds(536, 24, 141, 37);
		btnAddNewClient.setFont(new Font("Tahoma", Font.PLAIN, 11));
		frame.getContentPane().add(btnAddNewClient);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"SSN", "First Name", "Last name", "Address", "Phone Number", "Coach", "Subscription"}));
		comboBox.setBounds(463, 138, 114, 23);
		frame.getContentPane().add(comboBox);
		
		txtInputClientName = new JTextField();
		txtInputClientName.setBounds(123, 139, 130, 20);
		txtInputClientName.setText("Input cID/SSN");
		frame.getContentPane().add(txtInputClientName);
		txtInputClientName.setColumns(10);
		
		final JRadioButton rdbtnFindBycID = new JRadioButton("Find by cID");
		rdbtnFindBycID.setBounds(25, 138, 95, 23);
		frame.getContentPane().add(rdbtnFindBycID);
		
		final JRadioButton rdbtnFindBySsn = new JRadioButton("Find by SSN");
		rdbtnFindBySsn.setBounds(25, 164, 95, 23);
		frame.getContentPane().add(rdbtnFindBySsn);
		
		
		JButton btnFindClient = new JButton("Find Client");
		btnFindClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(rdbtnFindBycID.isSelected())
				{Model.Client c = ctr.findBycID(Integer.parseInt(txtInputClientName.getText()));
				TableModel model = new ClientTable(c);
				table_1.setModel(model);
				}
				if(rdbtnFindBySsn.isSelected())
				{Model.Client c = ctr.findBySSN(txtInputClientName.getText());
				TableModel model = new ClientTable(c);
				table_1.setModel(model);
				}
							
			}
		});

		
		btnFindClient.setBounds(263, 138, 130, 23);
		frame.getContentPane().add(btnFindClient);
		JButton btnDeleteSelected = new JButton("Delete Selected");
		btnDeleteSelected.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int x =table_1.getSelectedRow();
				int y =0;
				int del =(int) table_1.getValueAt(x, y);
				boolean b = ctr.deleteClient(del);
				if(b)
				lblStatus.setText("Deletion successful!");
				else
					lblStatus.setText("Failed!");
			}
		});
		btnDeleteSelected.setBounds(587, 172, 116, 23);
		frame.getContentPane().add(btnDeleteSelected);
		
		JButton btnNewButton = new JButton("Update Info");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = table_1.getSelectedRow();
				int y = 0;
				int cID = (int) table_1.getValueAt(x,y);
				boolean c = false;
				switch(comboBox.getSelectedIndex())
				{case 0: //SSN
					c=ctr.updateClient(cID,2,txtSingleupdate.getText(),0);
					break;
				case 1: //FNAME
					c=ctr.updateClient(cID, 0, txtSingleupdate.getText(), 0);
					break;
				case 2: //LNAME
					c=ctr.updateClient(cID, 1, txtSingleupdate.getText(), 0);
					break;
				case 3: //ADDRESS
					c=ctr.updateClient(cID, 3, txtSingleupdate.getText(), 0);
					break;
				case 4: //Phone
					c=ctr.updateClient(cID, 0, "", Integer.parseInt(txtSingleupdate.getText()));
					break;
				case 5: //Sub Type
					c=ctr.updateClient(cID, 1, "", Integer.parseInt(txtSingleupdate.getText()));
				case 6: //SubSessions
					c=ctr.updateClient(cID, 2, "", Integer.parseInt(txtSingleupdate.getText()));
					break;
				}
				if(c)
					lblStatus.setText("Success!");
				else
					lblStatus.setText("Failed!");
			}
		});
		btnNewButton.setBounds(589, 136, 114, 27);
		frame.getContentPane().add(btnNewButton);
		
	
		
		JButton btnListAll = new JButton("List ALL");
		btnListAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				TableModel model = new ClientTable();
				table_1.setModel(model);
			}
		});
		btnListAll.setBounds(263, 172, 130, 23);
		frame.getContentPane().add(btnListAll);
			
	
		
		
		JButton btnIllBeBach = new JButton("Close");
		btnIllBeBach.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
				
			}
		});
		btnIllBeBach.setBounds(302, 396, 156, 23);
		frame.getContentPane().add(btnIllBeBach);
		
	
		
		txtSingleupdate = new JTextField();
		txtSingleupdate.setText("SingleUpdate");
		txtSingleupdate.setBounds(463, 172, 114, 23);
		frame.getContentPane().add(txtSingleupdate);
		txtSingleupdate.setColumns(10);
		
		JLabel lblSession = new JLabel("Session");
		lblSession.setBounds(25, 100, 46, 14);
		frame.getContentPane().add(lblSession);
		
		textSession = new JTextField();
		textSession.setBounds(94, 99, 130, 20);
		frame.getContentPane().add(textSession);
		textSession.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 221, 707, 164);
		frame.getContentPane().add(scrollPane);
		
		table_1 = new JTable();
		table_1.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null, null},
			},
			new String[] {
				"Client ID", "SSN", "First Name", "Last Name", "Address", "Phone", "Sessions Left", "Sub Type"
			}
		));
		scrollPane.setViewportView(table_1);
		table_1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table_1.setCellSelectionEnabled(true);
		table_1.setColumnSelectionAllowed(true);
		
		Box horizontalBox = Box.createHorizontalBox();
		horizontalBox.setBorder(new LineBorder(new Color(0, 0, 0)));
		horizontalBox.setBounds(10, 11, 707, 114);
		frame.getContentPane().add(horizontalBox);
		
		Box horizontalBox_1 = Box.createHorizontalBox();
		horizontalBox_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		horizontalBox_1.setEnabled(false);
		horizontalBox_1.setBounds(10, 128, 421, 76);
		frame.getContentPane().add(horizontalBox_1);
		
		Box horizontalBox_2 = Box.createHorizontalBox();
		horizontalBox_2.setBorder(new LineBorder(new Color(0, 0, 0)));
		horizontalBox_2.setBounds(441, 128, 276, 76);
		frame.getContentPane().add(horizontalBox_2);
		
	}
}
